<?php
/**
 *zhouzhixiang@kuaidihelp.com
 *代码就像老婆，以后没人维护是多么恐怖是事情
 */

namespace soa\partner;
use artisan\cache;
use artisan\db;
use artisan\format;
use artisan\http;
use soa\check\EnvCheck;
use soa\common\RemoteDB;
use soa\partner\ane\Branch;
use soa\partner\zt\ztCommon;
use soa\service;

class partnerCommon
{
	const DB = 'kd_pda';

	protected static $table = [
		'sto' => 'tbl_scan_counterman',
		'zt'  => 'tbl_scan_courier_zt',
		'zto' => 'tbl_scan_courier_zt',
		'qf'  => 'tbl_scan_courier_qf',
		'ane' => 'tbl_scan_courier_ane',
		'kj'  => 'tbl_scan_courier_kj',
		'gt'  => 'tbl_scan_courier_gt',
	];

	/**
	 * 打印电子面单的同时需要调用的数据入库方法
	 *
	 * @param $data
	 * @return bool
	 */
	public function saveThermalPaperOrder($data)
	{
		if(empty($data) && !is_array($data)){
			raise(1009,'参数错误');
		}
//logdebug($data,'saveThermalPaperOrder_data');
		//订单编号
		$insertData['order_number'] 	= $data['order_number'];// ?  : raise(1009);
		//快递公司
		$insertData['brand'] 			= $data['brand'];// ?  : raise(1009) ;
		//网点名称
		$insertData['branch_name'] 		= $data['branch_name'];// ?  : raise(1009) ;
		//快递员id
		$insertData['cm_id'] 			= $data['cm_id'];// ?  : raise(1009) ;
		//快递员姓名
		$insertData['cm_name'] 			= $data['cm_name'];// ?  : raise(1009) ;
		//快递员手机号
		$insertData['cm_phone'] 		= $data['cm_phone'];// ?  : raise(1009);
		//快递单号
		$insertData['express_number']   = $data['express_number'];// ?  : raise(1009) ;
		//发件人姓名
		$insertData['sender_name'] 		= $data['sender_name'];// ?  : raise(1009) ;
		//发件人电话
		$insertData['sender_phone'] 	= $data['sender_phone'];// ?  : raise(1009) ;
		//发件人省
		$insertData['sender_province']  = $data['sender_province'];// ?  : raise(1009) ;
		//发件人市
		$insertData['sender_city'] 		= $data['sender_city'];// ?  : raise(1009) ;
		//发件人区
		$insertData['sender_area'] 		= $data['sender_area'];// ?  : raise(1009) ;
		//发件人详细地址
		$insertData['sender_address']   = $data['sender_address'];// ?  : raise(1009) ;
		//收件人姓名
		$insertData['receiver_name'] 	= $data['receiver_name'];// ?  : raise(1009) ;
		//收件人电话
		$insertData['receiver_phone']   = $data['receiver_phone'];// ?  : raise(1009) ;
		//收件人省
		$insertData['receiver_province']= $data['receiver_province'];// ?  : raise(1009) ;
		//收件人市
		$insertData['receiver_city'] 	= $data['receiver_city'];// ?  : raise(1009) ;
		//收件人区
		$insertData['receiver_area'] 	= $data['receiver_area'];// ?  : raise(1009) ;
		//收件人详细地址
		$insertData['receiver_address'] = $data['receiver_address'];// ?  : raise(1009) ;
		//电子面单打印时间
		$insertData['print_time'] 		= $data['print_time'];// ?  : date('Y-m-d H:i:s') ;
		//代收金额
		$insertData['collection_amount']= $data['collection_amount'];// ?  : '0.00' ;
		//计费重量
		$insertData['charging_weight']  = $data['charging_weight'] ?  : '' ;
		//物品类型(非必填)
		$insertData['goods_type'] 		= $data['goods_type'] ?  : '' ;
		//备注信息(非必填)
		$insertData['remark']			= $data['remark'] ? : '';
		

		//存入orders.tbl_thermalpaper_order数据库
		$ret = db::connect('orders')->table('tbl_thermalpaper_order')->insert($insertData);
		if($ret){
			//入库成功
			return true;
		}else{
			//打印电子面单数据入库失败，记录日志
			logDebug($ret,'saveThermalPaperOrderInsert');
			logdebug($insertData,'saveThermalPaperOrderData');
			return false;
		}
	}

	/**
	 * @param $brand
	 * @param $cmPhone
	 * @param $type
	 * @return string
	 */
	public static function cacheKeyBuilder($brand,$cmPhone,$type='')
	{
		if(empty($brand) || empty($cmPhone)){
			return '';
		}

		switch($type){
			case 'getScanInfo':

			case 'getAccessInfo':
				return $brand.'_access_info_:'.$cmPhone;

			case 'getEmpNoBranchNameByPhone':
				return $brand.'_emp_info_:'.$cmPhone;

			case 'getEmpNoByCmInfo':
				return $brand.'_emp_no_:'.$cmPhone;

			case 'getInfoByWaybillNo':
				return $brand.'waybill_info:'.$cmPhone;

			case 'getWeightInfoByWaybillNo':
				return $brand.'weight_info:'.$cmPhone;

			default:
				return '';
		}
	}

	/**
	 * 通过快递员手机号获取快递员巴枪数据(不管有没有禁用)
	 *
	 * @param $brand
	 * @param $cmPhone
	 * @return array|mixed
	 */
	public static function getScanLocalInfo($brand, $cmPhone)
	{
		if(empty($brand) || empty($cmPhone)){
			return [];
		}

		if('zt' == $brand){
			$where = [
				'cm_phone' => $cmPhone,
				'switch'   => 'on',
			];
		}else{
			$where = [
				'cm_phone' => $cmPhone,
				'valid'   => 1,
			];
		}
		return static::getScanInfo(['brand'=>$brand,'username'=>$cmPhone], $where);
	}

	/**
	 * 通过快递员手机号获取可用的快递员巴枪数据（无缓存）
	 *
	 * @param $courierInfo
	 * @param array $where
	 * @return array|mixed
	 */
	public static function getScanInfo($courierInfo, $where = [])
	{
		$brand   = !empty($courierInfo['brand']) ? $courierInfo['brand'] : '';
		$cmId    = !empty($courierInfo['cm_id']) ? $courierInfo['cm_id'] : '';
		$cmCode  = !empty($courierInfo['cm_code']) ? $courierInfo['cm_code'] : '';
		$cmPhone = !empty($courierInfo['cm_phone']) ? $courierInfo['cm_phone'] : '';
		$cmPhone = !empty($courierInfo['username'])? $courierInfo['username'] :$cmPhone;

		$ret = [];

		if(empty($brand) || !in_array($brand,array_keys(static::$table))){
			return $ret;
		}

		if(empty($where)){
			if($cmPhone){
				$setCache = 1;
				$where = array('cm_phone'=>$cmPhone);
			}elseif($cmId && 'zt' != $brand){
				$where = array('cm_id'=>$cmId);
			}elseif($cmCode){
				$where = 'zt' != $brand ? array('cm_code'=>$cmCode) : array('emp_no'=>$cmCode);
			}else{
				return [];
			}
		}

		if(in_array($brand,['ane','kj','gt'])){
			$orderBy = 'last_update_time desc';
		}else{
			$orderBy = '';
		}

		if ($brand == 'zt') {
			$condition = array('switch' => 'on', 'state' => 'approve');
			$condition['emp_no <>'] = '';
//			$condition['emp_no'] = 'IS NOT NULL';
		} else{
			$condition = array('valid' => 1, 'state' => 'approved');
			$condition['cm_code <>'] = '';
//			$condition['cm_code'] = 'IS NOT NULL';
		}
		$where = array_merge($where, $condition);

		$remoteDb = new RemoteDB(static::DB, static::$table[$brand]);
		$ret = $remoteDb->getOne($where, $orderBy);

		!empty($ret) and $ret['brand'] = $brand;
		!empty($ret['emp_no']) and $ret['cm_code'] = $ret['emp_no'];
		!empty($ret['emp_name']) and $ret['cm_name'] = $ret['emp_name'];
		!empty($ret['cm_code']) and $ret['emp_no'] = $ret['cm_code'];

		if(!empty($setCache)){
			$cacheKey = static::cacheKeyBuilder($brand, $cmPhone, __FUNCTION__);
			if(!empty($ret['emp_no'])){
				cache::connect('pda')->set($cacheKey, $ret,30);
			}else{
				cache::connect('pda')->set($cacheKey, [],5);
			}
		}

		return $ret;
	}

	/**
	 * 通过快递员手机号获取可用的快递员巴枪数据（缓存）
	 *
	 * @param $courierInfo
	 * @return array|bool|mixed|null|string
	 */
	public static function getAccessInfo($courierInfo)
	{
		$brand   = $courierInfo['brand'] ?: '';
		$cmPhone = $courierInfo['username']?:'';

		$ret = [];

		if(empty($cmPhone) || empty($brand)){
			return $ret;
		}

		$cacheKey = static::cacheKeyBuilder($brand, $cmPhone, __FUNCTION__);
		if(EnvCheck::isEnvOnline()){
			$ret = cache::connect('pda')->get($cacheKey);
		}else{
			$ret = false;
		}

		if(empty($ret) && $ret !==[]){
			$ret = static::getScanInfo($courierInfo);
		}

		return $ret;
	}

	/**
	 * 通过手机号获取业务员编号、网点名称
	 *
	 * @param $courierInfo
	 * @return array|bool|mixed|null|string
	 */
	public static function  getEmpNoBranchNameByPhone($courierInfo)
	{
		//$empInfo = array('empCode' => '', 'shopName' => '');
		$brand   = $courierInfo['brand'];
		$cmPhone = $courierInfo['username'];

		$cacheKey = static::cacheKeyBuilder($brand, $cmPhone, __FUNCTION__);
		$empInfo  = cache::connect('pda')->get($cacheKey);   //从缓存获取业务员编号

		if(empty($empInfo) || !is_array($empInfo)){
			$ret = static::getScanInfo($courierInfo);

			if($brand == 'sto'){
				if (!empty($ret['cm_code'])) {
					$empInfo['empCode']  = $ret['cm_code'];
					$empInfo['shopName'] = $ret['shop_name'];
				}
			}elseif($brand == 'zt'){
				if (!empty($ret['emp_no'])) {
					$empInfo['empCode']  = $ret['emp_no'];
					$empInfo['shopName'] = $ret['branch_name'];
				}
			}

			if($empInfo){
				$isOk = cache::connect('pda')->set($cacheKey, $empInfo,60);     //写入缓存
				if (!$isOk) {
					logDebug('用户id：' . $cmPhone . '缓存业务员编号失败:' . json_encode($isOk, JSON_UNESCAPED_UNICODE), 'deli_debug');
				}
			}
		}

		return $empInfo;
	}

	/**
	 * 根据手机号获取快递员的empNo
	 *
	 * @param $courierInfo
	 * @return array|bool|mixed|null|string
	 */
	public static function getEmpNoByCmInfo($courierInfo)
	{
		$brand   = $courierInfo['brand'];
		$cmPhone = $courierInfo['username'];

		$cacheKey = static::cacheKeyBuilder($brand, $cmPhone, __FUNCTION__);
		$EmpNo    = cache::connect('pda')->get($cacheKey);   //从缓存获取业务员编号

		if(empty($EmpNo)){
			$ret = static::getScanInfo($courierInfo);

			if(!empty($ret['emp_no'])){
				$EmpNo = $ret['emp_no'];
				cache::connect('pda')->set($cacheKey,$EmpNo,60);
			}
		}

		return $EmpNo;
	}

	/**
	 * 通过快递单号获取业务员信息
	 *
	 * @param $brand
	 * @param $waybillNo
	 * @return array|bool|mixed|null|string
	 */
	public static function getInfoByWaybillNo($brand,$waybillNo)
	{
		$cacheKey = static::cacheKeyBuilder($brand, $waybillNo, __FUNCTION__);
		$ret      = cache::connect('pda')->get($cacheKey);   //从缓存获取业务员编号

		if(empty($ret) && $ret !== []){
			if($brand == 'sto'){
				$remoteDb = new RemoteDB('kd_pda', 'tbl_scan_to_e3');
				$ret = $remoteDb->getOne(array('waybill_no' => $waybillNo),'steid desc');
//				$ret = \artisan\db::connect('kd_pda')->table('tbl_scan_to_e3')->select('cm_id,cm_phone,cm_name,cm_code,dev_id,dev_imei,operator_code,weight,latitude,longitude')->getOne(array('waybill_no' => $waybillNo),'steid desc');
			}elseif($brand == 'zt'){
				$remoteDb = new RemoteDB('kd_pda', 'tbl_scan_to_zt');
				$ret = $remoteDb->getOne(array('waybill_no' => $waybillNo),'steid desc');
//				$ret = \artisan\db::connect('kd_pda')->table('tbl_scan_to_zt')->select('emp_no')->getOne(array('waybill_no' => $waybillNo),'steid desc');
			}

			if (!empty($ret)) {
				$isOk = cache::connect('pda')->set($cacheKey, $ret,300);    //写入缓存
				if (!$isOk) {
					logDebug('快递单号：' . $waybillNo . '缓存快递单号对应的快递员信息失败:' . json_encode($isOk, JSON_UNESCAPED_UNICODE), 'deli_debug');
				}
			}else{
				cache::connect('pda')->set($cacheKey, [],30);
			}
		}

		return $ret;
	}

	/**
	 * 通过快递单号获取业务员信息
	 *
	 * @param $brand
	 * @param $waybillNo
	 * @return array|bool|mixed|null|string
	 */
	public static function getWeightInfoByWaybillNo($brand,$waybillNo)
	{
		$cacheKey = static::cacheKeyBuilder($brand, $waybillNo, __FUNCTION__);
		$ret      = cache::connect('pda')->get($cacheKey);   //从缓存获取业务员编号

		if(empty($ret) && $ret !== []){
			if(in_array($brand,['sto','ane'])){
				if('sto' == $brand){
					$table = 'tbl_scan_to_e3';
				}else{
					$table = 'tbl_scan_to_ane';
				}
				$remoteDb = new RemoteDB('kd_pda', $table);
				$ret = $remoteDb->getOne(array('waybill_no' => $waybillNo,'waybill_type'=>1),'steid desc');
//				$ret = \artisan\db::connect('kd_pda')->table('tbl_scan_to_e3')->select('cm_id,cm_phone,cm_name,cm_code,dev_id,dev_imei,operator_code,weight,latitude,longitude')->getOne(array('waybill_no' => $waybillNo,'waybill_type'=>1),'steid desc');
				if (!empty($ret)) {
					$isOk = cache::connect('pda')->set($cacheKey, $ret,300);    //写入缓存
					if (!$isOk) {
						logDebug('快递单号：' . $waybillNo . '缓存快递单号对应的快递员信息失败:' . json_encode($isOk, JSON_UNESCAPED_UNICODE), 'deli_debug');
					}
				}else{
					$isOk = cache::connect('pda')->set($cacheKey, [],10);
				}
			}elseif($brand == 'zt'){
				$ret = [];
			}
		}

		return $ret;
	}

	/**
	 * 申通由index_shop_id获得网点code
	 *
	 * @param $MainID
	 * @return bool|mixed|null|string
	 */
	public static function getStoBranchCodeByMainID($MainID)
	{
		$branchCode = cache::connect('pda')->get('PI-BranchCode:'.$MainID);
		if(empty($branchCode)) {

			$branchInfo = db::connect('kd_shop')->table('tbl_branch_list')
				->select('BranchCode')
				->getOne(array('MainID' => $MainID, 'brand' => 'sto'),'id desc');

			$branchCode = $branchInfo['BranchCode'];

			if(!empty($branchId)) {
				cache::connect('pda')->set('PI-BranchCode:'.$branchCode, $branchCode, 86400);
			}
		}
		return $branchCode;
	}

	/**
	 * 中通由empNo获得网点id
	 *
	 * @param $empNo
	 * @return bool|mixed|null|string
	 */
	public static function getZtBranchIdByEmpNo($empNo)
	{
		$branchId = cache::connect('pda')->get('PI:BRANCHID:'.$empNo);
		if(empty($branchId)) {
			$branchId = '';
			//首先根据empNo获取branch_code
			$branch_code = substr($empNo, 0, stripos($empNo, '.'));
			if(!empty($branch_code)) {
				$branchInfo = db::connect('kd_shop')->table('tbl_branch_list')
					->select('branch_id')
					->getOne(array('BranchCode' => $branch_code, 'brand' => 'zt'));

				$branchId = $branchInfo['branch_id'];
			}

			if(!empty($branchId)) {
				cache::connect('pda')->set('PI:BRANCHID:'.$empNo, $branchId, 2 * 86400);
			}
		}
		return $branchId;
	}

	/**
	 * 各品牌由网点编号branchCode获取网点信息
	 *
	 * @param $branchCode
	 * @param string $brand
	 * @return bool|mixed|null|string
	 */
	public static function getBranchByBranchCode($branchCode, $brand = '')
	{
		if($brand){
			$cacheKey = $brand . 'PI:BRANCH:'.$branchCode;
			$branchInfo = cache::connect('pda')->get($cacheKey);
		}

		if(empty($branchInfo)) {
			if(!empty($branchCode)) {
				$branchInfo = service::branch()->getBranchByCode($branchCode, $brand);
			}

			if(!empty($branchInfo) && !empty($cacheKey)) {
				cache::connect('pda')->set($cacheKey, $branchInfo, 10);
			}
		}
		return !empty($branchInfo) ? $branchInfo : '';
	}

	/**
	 * 从tbl_branch_list查询网点信息（批量）
	 *
	 * @param $data
	 * @return array|bool|int|mixed
	 */
	public static function getBranches($data)
	{
		if(empty($data['brand']) && empty($data['branch_code']) && empty($data['branch_name'])){
			return [];
		}

		//暂时处理，等国通把基础资料维护好再走原来的逻辑
		if('gt' == $data['brand']) {
			return self::getGtBranches($data);
		}

		$isLike = empty($data['isLike']) ? 0 : 1;

		if(isset($data['brand'])){
			$where['brand'] = $data['brand'];
		}
		if(isset($data['branch_code'])){
			if(is_array($data['branch_code'])){
				$where['BranchCode in'] = $data['branch_code'];
			}else{
				if($isLike){
					$where['BranchCode like'] = '%' . $data['branch_code'] . '%';
				}else{
					$where['BranchCode'] = $data['branch_code'];
				}
			}
		}elseif(isset($data['branch_name'])){
			if(is_array($data['branch_name'])){
				$where['name in'] = $data['branch_name'];
			}else{
				if($isLike){
					$where['name like'] = '%' . $data['branch_name'] . '%';
				}else{
					$where['name'] = $data['branch_name'];
				}
			}
		}

		//暂时，等有service再调service
		return !empty($where) ? (new RemoteDB('kd_shop','tbl_branch_list'))->getAll($where) : [];
	}

	/**
	 * 从tbl_branch_list查询网点信息（单个）
	 *
	 * @param $data
	 * @return array|mixed
	 */
	public static function getBranch($data)
	{
		if(empty($data['brand']) && empty($data['branch_code']) && empty($data['branch_name'])){
			return [];
		}

		if(isset($data['brand'])){
			$where['brand'] = $data['brand'];
		}
		if(isset($data['branch_code'])){
			$where['BranchCode'] = $data['branch_code'];
		}elseif(isset($data['branch_name'])){
			$where['name'] = $data['branch_name'];
		}

		//暂时，等有service再调service
		return !empty($where) ? (new RemoteDB('kd_shop','tbl_branch_list'))->getOne($where) : [];
	}

	/**
	 * 从第三方接口查询网点信息
	 *
	 * 申通：从tbl_branch_list查询
	 * 中通、安能：有接口查询网点信息
	 *
	 * @param $brand
	 * @param $branchCode
	 * @param array $data
	 * @return array|string
	 */
	public static function getBranchFromThird($branchCode, $brand, &$data = [])
	{
		if(empty($brand) || empty($branchCode)){
			return [];
		}

		$key = $brand.'PI:BRANCH_INFO:'.$branchCode;
		$branchInfo = cache::connect('pda')->get($key);
		if($branchInfo && is_array($branchInfo)){
			return $branchInfo;
		}

		if('zt' == $brand){
			$cmPhone = !empty($data['username']) ? $data['username'] : (!empty($data['cm_phone']) ? $data['cm_phone'] : '');
			if(empty($data) || empty($cmPhone)){
				return [];
			}

			$ret = (new ztCommon())->ztXMLInterface($cmPhone,['optype'=>'G002','data'=>'<a>2001-01-01 00:00:00</a>'],1);
			if(isset($ret['Success']['d']['p']['b'])) {
				$ret = [
					'BranchCode' => $ret['Success']['d']['p']['a'],
					'name' => $ret['Success']['d']['p']['b']
				];
				cache::connect('pda')->set($key,$ret,5);
				return $ret;
			}else{
				logDebug($ret,'zt_get_branch_fail');
			}
		} elseif('ane' == $brand){
			$branch = new Branch();
			$result = $branch->getBranchDetail($branchCode);
			if($result && is_array($result)){
				$ret = (isset($result['siteId']) && $result['siteName']) ? [
					'BranchCode'   => $branchCode,
					'branch_id'    => $result['siteId'],
					'name'         => $result['siteName'],
					'site_type_id' => $result['siteTypeId'],
				] : [];
				$ret and cache::connect('pda')->set($key,$ret,5);
				return $ret;
			}else{
				logDebug($branch->getResult(),'ane_get_branch_fail',$branchCode);
				return [];
			}
		}
		return [];
	}

	/**
	 * 由巴枪工号cmCode查快递员信息
	 *
	 * @param $brand
	 * @param $cmCode
	 * @return array|bool|mixed|null|string
	 */
	public static function getCmInfoByCmCode($cmCode, $brand)
	{
		$cacheKey = $brand . 'PI:COUNTER_MAN:'.$cmCode;
		$cmInfo = cache::connect('pda')->get($cacheKey);

		if(empty($cmInfo)) {
			if($cmCode && $brand) {
				$cmInfo = (new Counterman)->counterman(['brand'=>$brand,'cm_code'=>$cmCode]);
				if($cmInfo && is_array($cmInfo)){
					$cmInfo = array_shift($cmInfo);
				}
			}

			if($cmInfo) {
				cache::connect('pda')->set($cacheKey, $cmInfo, 10);
			}
		}

		return !empty($cmInfo) ? $cmInfo : [];
	}

	/**
	 * 从第三方接口查询快递员信息
	 *
	 * 申通、安能：接口同步快递员信息，快递员信息从tbl_counter_man表查
	 * 中通：有接口获取该网点所有快递员信息
	 * 全峰：有接口由手机号查询单个快递员信息
	 *
	 * @param $brand
	 * @param $code
	 * @param array $data
	 * @return array
	 */
	public static function getCmInfoFromThird($code, $brand, $data = [])
	{
		$return = [];

		if('zt' == $brand) {
			$cmPhone = !empty($data['username']) ? $data['username'] : (!empty($data['cm_phone']) ? $data['cm_phone'] : '');
			if(empty($data) || empty($cmPhone)){
				return [];
			}

			$branch = substr($code, 0, stripos($code, '.'));
			$cacheKey = 'zt_user_' . $branch;
			$retInfo = cache::connect('pda')->get($cacheKey);
			if(empty($retInfo)){
				$ret = (new ztCommon())->ztXMLInterface($cmPhone,['optype'=>'G003','data'=>'<a>2001-01-01 00:00:00</a>'],1);
				if(isset($ret['Success']['d']['p']) && $ret['Success']['d']['p']) {
					$retInfo = [];
					if(isset($ret['Success']['d']['p']['a'])) { //这里兼容只有一个员工返回的数据格式不同于多个的情况
						$retInfo[$ret['Success']['d']['p']['b']] = $ret['Success']['d']['p'];
					} else {
						foreach ($ret['Success']['d']['p'] as $v) {
							$retInfo[$v['b']] = $v;
						}
					}
					if($retInfo){
						cache::connect('pda')->set($cacheKey,$retInfo,600);
					}
				}
			}

			if(empty($retInfo[$code])){
				return [];
			}

			$return = [
				"shop_name"       => $retInfo[$code]['a'],
				"counterman_code" => $retInfo[$code]['b'],
				"counterman_name" => $retInfo[$code]['c'],
				"phone"           => is_array($retInfo[$code]['g']) ? strval(array_pop($retInfo[$code]['g'])) : strval($retInfo[$code]['g'])
			];
		}elseif('qf' == $brand){
			$url = "http://orderservice.qfkd.com.cn/getusersite.asmx/GetUserSiteName?key=/cS3LIJ85RlWXldpkPKerA==&telPhone=" . $code;

			$data = http::get($url);
			$ret = format::xml2array( str_replace("\r\n", '', $data) );

			if ($ret['string'] == '没有数据') {

			} else {
				$retInfo = explode(';', $ret['string']);
				if (!empty($retInfo[0])) {
					$return = [
						'shop_name'       => $retInfo[3],
						'counterman_code' => $retInfo[0],
						'counterman_name' => $retInfo[1],
						'phone'           => preg_match('/^[\d\-]+$/i', $code) ? $code : ''
					];
				}
			}
		}

		return $return;
	}

	/**
	 * @param $data
	 * @return array|bool|int|mixed
	 */
	public static function getGtBranches($data)
	{
		$where['type'] = '1';
		if($data['branch_code']){
			$where['site_code'] = $data['branch_code'];
		}elseif($data['branch_name']){
			$where['site_name like'] = '%' . $data['branch_name'] . '%';
		}

		return db::connect(self::DB)->table('tbl_site_query_gt')->select('id as branch_id,site_code as BranchCode,site_name as name')->getAll($where);
	}

	/**
	 * @param $id
	 * @return string
	 */
	public static function getProfilePic($id)
	{
		if(empty($id)){
			return '';
		}
		return 'http://upload.kuaidihelp.com/touxiang/counterman_' . $id . '.jpg';
	}
}

